﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Linq;
using BemEstarEmprego.BLL;
using BemEstarEmprego.BLL.Models;
using BemEstarEmprego.Entities;
//using BemEstarEmprego.Web.Models;
using BemEstarEmprego.Web.Helpers;

namespace BemEstarEmprego.Web.Controllers
{
	[SecurityFilter]
	public class VagaController : BaseController
	{
		// GET: /Candidato/

		public ActionResult Index()
		{
			return View();
		}

		public ActionResult Cadastro()
		{
			ViewBag.Action = "Salvar";
			VagaModel model = new VagaModel() { tipoDeficiencia = PopulaDropDownList(0) };
			return View(model);
		}

		public ActionResult Autocomplete(string term)
		{
			var items = new VagaBLL().BuscaEmpresas(term);

			var filteredItems = items.Where(
				item => item.IndexOf(term, StringComparison.InvariantCultureIgnoreCase) >= 0
				);
			return Json(filteredItems, JsonRequestBehavior.AllowGet);

		}

		[HttpPost]
		[MultiButton(MatchFormKey = "action", MatchFormValue = "Salvar")]
		public ActionResult Salvar(VagaModel model, String tipDeficiencia)
		{
			VagaBLL vagaBLL = new VagaBLL();
			model.tipoDeficienciaId = Convert.ToInt64(tipDeficiencia);
			var retornoSave = vagaBLL.Save(model);

			return RedirectToAction("list");
		}

		[HttpPost]
		[MultiButton(MatchFormKey = "action", MatchFormValue = "Voltar")]
		public ActionResult Voltar()
		{
			return RedirectToAction("list");
		}

		public ActionResult Editar(String id)
		{
			Int32 _id = Convert.ToInt32(id);
			VagaModel model = new VagaModel();

			ViewBag.Action = "Alterar";

			var lstTipoDeficiencia = (from item in new bemestar_empregoEntities().tipo_deficiencia select item).ToList();

			using (bemestar_empregoEntities context = new bemestar_empregoEntities())
			{
				var vaga = context.vagas.Where(x => x.ID == _id).FirstOrDefault<vaga>();
				vaga.tipo_deficienciaReference.Load();
				model._vaga = vaga;

				model.tipoDeficiencia = PopulaDropDownList(vaga.tipo_deficiencia.ID);

				return View(model);
			}
		}

		private IEnumerable<SelectListItem> PopulaDropDownList(Int64 id)
		{
			return new bemestar_empregoEntities().tipo_deficiencia
																	.AsEnumerable()
																	.Select(x => new SelectListItem()
																	{
																		Selected = (x.ID == id),
																		Value = x.ID.ToString(),
																		Text = x.DESCRICAO
																	});
		}

		[HttpPost]
		[MultiButton(MatchFormKey = "action", MatchFormValue = "Alterar")]
		public ActionResult Alterar(CandidatoModel model)
		{
			CandidatoBLL candidato = new CandidatoBLL();
			candidato.UpdateCandidato(model._pessoa);

			EnderecoBLL endereco = new EnderecoBLL();
			endereco.UpdateEndereco(model._endereco);

			return RedirectToAction("list");
		}

		public ActionResult List()
		{
			string pageString = "";

			try
			{
				pageString = Request.Url.Segments[3];
			}
			catch (Exception)
			{
				pageString = null;
			}

			int page = (String.IsNullOrEmpty(pageString)) ? 1 : Int32.Parse(pageString);

			int totalVaga = new VagaModel().GetTotalVagas();

			Extension.Pagination pagination = new Extension.Pagination(true);

			pagination.BaseUrl = "/vaga/List/";
			pagination.TotalRows = totalVaga;
			pagination.CurPage = page;
			pagination.PerPage = 10;

			pagination.PrevLink = "Prev";
			pagination.NextLink = "Next";

			string pageLinks = pagination.GetPageLinks();

			int start = (page - 1) * pagination.PerPage;
			int offset = pagination.PerPage;

			List<vaga> vagas = new VagaModel().GetVagas(start, offset);

			ViewData["title"] = "Pagination in Asp.Net Mvc";
			ViewData["totalVaga"] = totalVaga;
			ViewData["vagas"] = vagas;
			ViewData["pageLinks"] = pageLinks;
			ViewBag.Links = pageLinks;

			return View("Consulta");
		}

	}
}
